﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.AccessControl;

namespace SPVisualDev.Services
{
    public class PushDown
    {

        //PathKind: 1 = File, 2 = Directory
        public static bool PathExistsIn12Hive(string ProjectRelativePath, int PathKind)
        {
            if (!ProjectRelativePath.StartsWith("12\\"))
                throw new Exception("Not inside 12 hive.");

            ProjectRelativePath = ProjectRelativePath.Substring(3);

            string system_12_hive = SharePointLibrary.GetSharePointInstallPath();
            string dest = system_12_hive + "\\" + ProjectRelativePath;

            if (PathKind == 1)
            {
                return File.Exists(dest);
            }
            else
            {
                return Directory.Exists(dest);
            }
        }

        public static string GetProjectItem12HivePath(string ProjectRelativePath)
        {
            if (!ProjectRelativePath.StartsWith("12\\"))
                throw new Exception("Not inside 12 hive.");

            ProjectRelativePath = ProjectRelativePath.Substring(3);

            string path = SharePointLibrary.GetSharePointInstallPath() + "\\" + ProjectRelativePath;

            return path;
        }

        public static void DeletePathIn12Hive(string ProjectRelativePath, int PathKind)
        {
            if (PushDown.PathExistsIn12Hive(ProjectRelativePath, PathKind))
            {
                if (!ProjectRelativePath.StartsWith("12\\"))
                    throw new Exception("Not inside 12 hive.");

                ProjectRelativePath = ProjectRelativePath.Substring(3);

                string system_12_hive = SharePointLibrary.GetSharePointInstallPath();
                string dest = system_12_hive + "\\" + ProjectRelativePath;

                if (PathKind == 1)
                    File.Delete(dest);
                else
                    Directory.Delete(dest, true);
            }
        }

        public static void CreatePathIn12Hive(string ProjectRelativePath)
        {
            if (ProjectRelativePath.StartsWith("12\\"))
            {
                ProjectRelativePath = ProjectRelativePath.Substring(3);
            }
            else if (ProjectRelativePath == "12")
            {
                ProjectRelativePath = "";
            }
            else
            {
                throw new Exception("Not inside 12 hive.");
            }

            string system_12_hive = SharePointLibrary.GetSharePointInstallPath();
            string dest = system_12_hive;

            string[] arr = ProjectRelativePath.Split('\\');
            foreach (string s in arr)
            {
                dest += "\\" + s;
                if (!Directory.Exists(dest))
                {
                    Directory.CreateDirectory(dest);

                    DirectoryInfo dirInfo = new DirectoryInfo(dest);
                    DirectorySecurity dirSecurity = dirInfo.GetAccessControl();
                    dirSecurity.SetAccessRuleProtection(false, false);
                    dirInfo.SetAccessControl(dirSecurity);

                }
            }
        }

    }
}
